//
//  PullRefreshManager.m
//  TooToo
//
//  Created by liuning on 15/10/22.
//  Copyright © 2015年 MoHao. All rights reserved.
//

#import "PullRefreshManager.h"
//#import "SVPullToRefresh.h"
#import "MJRefresh.h"
#import "VMRefreshHeader.h"

@interface PullRefreshManager ()
{
    NSInteger originPage;
    NSInteger temPage;
    
    BOOL needPullDownRefresh;
    BOOL needLoadMoreRefresh;
}
@end;

@implementation PullRefreshManager;


-(void)dealloc
{
    NSLog(@"PullRefreshManager dealloc");
}

- (instancetype)init
{
    self = [super init];
    
    if (self) {
    }
    return self;
}

-(void)addScrollView:(UIScrollView *)scrollView needPullDown:(BOOL)needPullDown needLoadMore:(BOOL)needLoadMore
{
        
    self.scrollView = scrollView;
    self.scrollView.alwaysBounceVertical = YES;
    
    needPullDownRefresh = needPullDown;
    needLoadMoreRefresh = needLoadMore;
    
    originPage = 1;
    temPage = originPage;
    
    [self refreshCurPageToOrigin];
    
    __weak PullRefreshManager * weakSelf = self;
    
    if (needPullDown) {
        
        self.scrollView.mj_header = [VMRefreshHeader headerWithRefreshingBlock:^{
            [weakSelf pullDownRefreshAction];
        }];
        
        
        
    }
    
    if (needLoadMore) {
        
        self.scrollView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            [weakSelf loadNextAction];
        }];
        
        self.scrollView.mj_footer.automaticallyHidden =YES;
    
    }
}

-(void)removeRefreshView
{
//    [self.scrollView.mj_header removeFromSuperview];
//    [self.scrollView.mj_footer removeFromSuperview];
    self.scrollView.mj_header = nil;
    self.scrollView.mj_footer = nil;
}


-(void)pullDownRefreshAction
{
    _curPageIndex = originPage;
    temPage = _curPageIndex;
    
    if (self.refreshAction) {
        self.refreshAction();
    }
}
-(void)loadNextAction
{
    temPage = _curPageIndex;
    _curPageIndex++;
    
    if (self.loadMoreAction) {
        self.loadMoreAction();
    }
}
-(void)refreshCurPageToOrigin
{
    _curPageIndex = originPage;
}
-(void)requestSucceed
{
    //结束下拉Loading的动画
    [self.scrollView.mj_header endRefreshing];
    [self.scrollView.mj_footer endRefreshing];
    
}
-(void)requestFaild
{
    //结束下拉Loading的动画
    [self.scrollView.mj_header endRefreshing];
    [self.scrollView.mj_footer endRefreshing];
    
    //页码回到请求前状态
    _curPageIndex = temPage;
}
-(void)judgeTheEndByCurrentNum:(NSInteger)currentNum andTotalNum:(NSInteger)totalNum
{
    if (totalNum == 0) {
        self.scrollView.mj_footer.automaticallyHidden =YES;
    }else{
        
        self.scrollView.mj_footer.automaticallyHidden =NO;
    }
    
    if (totalNum<=currentNum) {
        
        [self reachedTheEnd:YES];
    }else{
        [self reachedTheEnd:NO];
    }
}
-(void)reachedTheEnd:(BOOL)end
{
    if (end) {
        
        [self.scrollView.mj_footer endRefreshingWithNoMoreData];
    }
    
}


-(BOOL)isActive
{
    
    if (self.scrollView.mj_header.isRefreshing || self.scrollView.mj_footer.isRefreshing) {
        
        return YES;
    }
    return NO;
}
-(BOOL)isRefreshSucceed
{
    if (_curPageIndex <= originPage) {
        return YES;
    }
    return NO;
}

-(void)addTopLogo
{
    //图片的长宽比为10：1
//    float height = self.scrollView.bounds.size.width/10.0f;
//    
//    UIImageView * imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, -height-SVPullToRefreshViewHeight, self.scrollView.bounds.size.width, height)];
//    imgView.image = [UIImage imageNamed:@"commonkit_pullrefresh_topLogo"];
//    [self.scrollView addSubview:imgView];
}


@end
